package org.example.websocket.controller;




import org.example.websocket.constants.ComConstants;
import org.example.websocket.domain.MsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/****
 * @author songshiming
 * @date 2023/1/3
 * @desc
 */

@Controller
@RequestMapping("/")
public class WebSocketController
{
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    //推送数据接口
    @ResponseBody
    @RequestMapping("/push/{sid}")
    public Map<String, Object> pushToWeb(@PathVariable String sid, String message) {
        if (message == null) {
            message = "我是消息!!!";
        }
        Map<String, Object> result = new HashMap<>();
        // 组装发送的消息  模拟 一个服务推送消息到前端
        MsgVo msgVo = new MsgVo();
        msgVo.setSid(sid);
        msgVo.setMessage(message);

        redisTemplate.convertAndSend(ComConstants.TOPIC_CHANNEL_SEND, msgVo);
        result.put("code", 200);
        result.put("msg", "success");
        return result;
    }

}
